package com.ran.java_gobang_new.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ran.java_gobang_new.mapper.UserInfoMapper;
import com.ran.java_gobang_new.my_exception.UserInfoException;
import com.ran.java_gobang_new.pojo.dao.UserInfo;
import com.ran.java_gobang_new.pojo.request.UserInfoLoginRequest;
import com.ran.java_gobang_new.pojo.request.UserInfoRegisterRequest;
import com.ran.java_gobang_new.pojo.response.UserInfoGameHallReponse;
import com.ran.java_gobang_new.pojo.response.Result;
import com.ran.java_gobang_new.pojo.response.UserInfoLoginResponse;
import com.ran.java_gobang_new.pojo.response.UserInfoRegisterResponse;
import com.ran.java_gobang_new.utils.BeanTrans;
import com.ran.java_gobang_new.utils.JsonTrans;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-09-02
 * Time: 23:10
 */
@Slf4j
@Service
public class UserInfoServise {
    @Autowired
    private UserInfoMapper userInfoMapper;

    public Object login(UserInfoLoginRequest userInfoLoginRequest, HttpServletRequest request) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(UserInfo::getUserName,userInfoLoginRequest.getUserName());
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo == null) {
            log.error("用户: " + userInfoLoginRequest.getUserName() + " 不存在!");
            return Result.fail("用户名错误");
        }
        if (!userInfo.getPassword().equals(userInfoLoginRequest.getPassword())) {
            log.error("用户: " + userInfoLoginRequest.getUserName() + " 密码错误!");
            return Result.fail("密码错误");
        }
        // 该用户存在, 账号密码都正确
        log.info("用户: " + userInfoLoginRequest.getUserName() + " 登陆成功!");
        HttpSession session = request.getSession(true); // 创建一个空的session用来放 用户 信息
        session.setAttribute("user",userInfo); // 将用户信息放进去, 为后面做准备
        UserInfoLoginResponse trans = BeanTrans.trans(userInfoLoginRequest);

        return Result.success(trans,"登陆成功!, 欢迎您: " + userInfo.getUserName());
    }


    public Object getUserInfo(HttpServletRequest request) {

        try {
            HttpSession session = request.getSession(false);
            UserInfo user = (UserInfo) session.getAttribute("user"); // 从会话中取出该用户
            // 把该用户转化为特定的 展示对象
            // 从数据库中查, 更新大厅信息
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(UserInfo::getUserName,user.getUserName());
            UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);

            return Result.success(BeanTrans.trans(userInfo));
        }catch (NullPointerException e) {
            log.error("空用户非法请求大厅");
            throw new UserInfoException("空用户请求大厅!");
        }

    }

    public void win(int winnerId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getUserId,winnerId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);

        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(UserInfo::getScore,userInfo.getScore() + 20)
                .set(UserInfo::getTotalCount,userInfo.getTotalCount() + 1)
                .set(UserInfo::getWinCount,userInfo.getWinCount() + 1)
                .eq(UserInfo::getUserId,winnerId);
        userInfoMapper.update(updateWrapper);
    }

    public void lose(Integer loserId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getUserId,loserId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);

        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(UserInfo::getScore,userInfo.getScore() - 10)
                .set(UserInfo::getTotalCount,userInfo.getTotalCount() + 1)
                .eq(UserInfo::getUserId,loserId);
        userInfoMapper.update(updateWrapper);
    }

    public UserInfoRegisterResponse register(UserInfoRegisterRequest request) {
        UserInfoRegisterResponse response = new UserInfoRegisterResponse();
        if (request == null) {
            throw new UserInfoException("注册信息为空!");
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(UserInfo::getUserName,request.getUserName());
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        // 已经被其他人注册
        if (userInfo != null) {
            response.setOk(false);
            response.setMessage("该用户已经注册, 请换一个用户名!");
            return response;
        }
        // 还没注册
        UserInfo user = new UserInfo();
        user.setUserName(request.getUserName());
        user.setPassword(request.getPassword());
        user.setScore(1000);
        user.setTotalCount(0);
        user.setWinCount(0);
        user.setDraw(0);
        userInfoMapper.insert(user);
        response.setOk(true);
        response.setMessage("注册成功!");
        return response;
    }
}
